import React, { PureComponent } from 'react';
import moment from 'moment';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message } from 'antd';
import { ModalC } from '../../../comps';
import { connect } from 'dva';
import OperateFormC from './operateFormC';

@Form.create()
@connect(({ bondInformationM, loading }) => ({ bondInformationM, loading }))
export default class UpdateModalC extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      secucategoryTypeList: [], //债券类型下拉列表
      issuerList: [], //发行人下拉列表
      tempIssuerList: [], //筛选后的发行人下拉列表
      bondRatingList: [], //债券评级下拉列表
      issuerRatingList: [], //主体评级下拉列表
    };
  }
  componentDidMount() {}

  // 修改visible
  changeVisible = (msg) => {
    this.props.changeVisible(msg);
  };

  // 模态框确认
  operateOnOk = () => {
    const {
      form: { validateFields, resetFields, getFieldsValue },
    } = this.$operateFormC.props;
    const formValues = getFieldsValue();
    // return
    if (!(formValues.listingEnddate.format('YYYY-MM-DD') >= formValues.listingStartdate.format('YYYY-MM-DD'))) {
      //发行截止日 >= 发行起始日
      message.error('发行截止日期不能早于发行起始日期');
      return;
    }
    validateFields(
      [
        'tradingCode',
        'secuabbr',
        'secucategoryCodeii',
        'exchangeCode',
        'bondMaturity',
        'couponRate',
        ,
        'listingStartdate',
        'listingEnddate',
        'pickingDate',
        'fundingDate',
        'issuer',
        'bondRating',
        'issuerRating',
        'issueSize',
        'prepaindProp',
        'prepaindAmount',
        'supplementaryTool',
        'termUnit',
      ],
      (err, fieldsValue) => {
        if (err) return;
        if (!(!(fieldsValue.prepaindProp || fieldsValue.prepaindProp === 0) === !!(fieldsValue.prepaindAmount || fieldsValue.prepaindAmount === 0))) {
          message.error('定金和定金比例二选一必填');
          return;
        }
        const value = {
          id: this.props.isEdit == '2' ? this.props.editObj.id : undefined,
          tradingCode: fieldsValue.tradingCode, //债券代码
          secuabbr: fieldsValue.secuabbr, //债券名称
          secucategoryCodeii: fieldsValue.secucategoryCodeii.value, //债券类型代码
          exchangeCode: fieldsValue.exchangeCode, //市场代码
          bondMaturity: fieldsValue.bondMaturity - 0, //期限
          termUnit: fieldsValue.termUnit, //期限
          couponRate: Number((fieldsValue.couponRate - 0) / 100).toFixed(4), //利率
          listingStartdate: moment(fieldsValue.listingStartdate).format('YYYY-MM-DD'), //发行起始日
          listingEnddate: moment(fieldsValue.listingEnddate).format('YYYY-MM-DD'), //发行截止日
          pickingDate: moment(fieldsValue.pickingDate).format('YYYY-MM-DD'), //中签日
          fundingDate: moment(fieldsValue.fundingDate).format('YYYY-MM-DD'), //缴款日
          issuer: fieldsValue.issuer == this.props.editObj.issuerName ? this.props.editObj.issuer : fieldsValue.issuer,
          bondRating: fieldsValue.bondRating, //债项评级
          issuerRating: fieldsValue.issuerRating, //主体评级
          issueSize: Number((fieldsValue.issueSize - 0) * 10000).toFixed(2), //发行总量
          supplementaryTool: fieldsValue.supplementaryTool,
        };
        if (fieldsValue.prepaindProp || fieldsValue.prepaindProp === 0) {
          value.prepaindProp = Number((fieldsValue.prepaindProp - 0) / 100).toFixed(4);
        } else if (fieldsValue.prepaindAmount || fieldsValue.prepaindAmount === 0) {
          value.prepaindAmount = Number((fieldsValue.prepaindAmount - 0) * 10000).toFixed(2);
        }
        if (this.props.isEdit == '2') {
          //修改状态下
          this.props.dispatch({
            type: 'bondInformationM/editSecucategoryById',
            payload: value,
            callback: (res) => {
              if (res) {
                resetFields();
                this.changeVisible(false);
              }
              this.props.useGetTableData(true);
            },
          });
        } else {
          // 新增状态下
          this.props.dispatch({
            type: 'bondInformationM/addSecucategory',
            payload: value,
            callback: (res) => {
              if (res) {
                resetFields();
                this.changeVisible(false);
              }
              this.props.useGetTableData(true);
            },
          });
        }
      },
    );
  };
  // 模态框取消
  addanCancel = () => {
    const {
      form: { resetFields },
    } = this.$operateFormC.props;
    this.changeVisible(false);
    resetFields();
  };
  // 上市转换确认
  MarketOnOk = () => {
    const {
      form: { validateFields, resetFields },
    } = this.$operateFormC.props;
    validateFields(
      ['secucategoryCodeii', 'secucategoryCodeiiMarket', 'exchangeCodeMarket', 'exchangeCode', 'issuerMarket', 'issuer', 'tradingCode', 'marketValue'],
      (err, fieldsValue) => {
        if (err) return;
        // 债券类型、交易市场和发行人在发行信息和上市信息中应保持一致
        if (
          fieldsValue.secucategoryCodeiiMarket &&
          fieldsValue.exchangeCodeMarket &&
          fieldsValue.issuerMarket &&
          fieldsValue.secucategoryCodeiiMarket == fieldsValue.secucategoryCodeii.value &&
          fieldsValue.exchangeCodeMarket == fieldsValue.exchangeCode &&
          fieldsValue.issuerMarket == fieldsValue.issuer
        ) {
          this.props.dispatch({
            type: 'bondInformationM/changeSecucode',
            payload: {
              beforSecucode: fieldsValue.tradingCode ? fieldsValue.tradingCode : undefined,
              tradingcode: fieldsValue.marketValue ? fieldsValue.marketValue.split('-')[1] : undefined,
              exchangecode: fieldsValue.marketValue ? fieldsValue.marketValue.split('-')[0] : undefined,
            },
            callback: (type) => {
              if (type == 'success') {
                resetFields();
                this.changeVisible(false);
              }
            },
          });
        } else {
          message.warning('债券类型、交易市场和发行人在发行信息和上市信息中应保持一致');
        }
      },
    );
  };
  render() {
    return (
      <ModalC
        title={this.props.isEdit == '2' ? '修改' : this.props.isEdit == '1' ? '新建' : '上市转换'}
        visible={this.props.visible}
        maskClosable={false}
        onOk={this.props.isEdit == '3' ? this.MarketOnOk : this.operateOnOk}
        onCancel={this.addanCancel}
        confirmLoading={
          this.props.loading.effects['bondInformationM/changeSecucode'] == true ||
          this.props.loading.effects['bondInformationM/editSecucategoryById'] == true ||
          this.props.loading.effects['bondInformationM/addSecucategory'] == true
        }
        width={1200}
      >
        <OperateFormC
          editObj={this.props.editObj}
          isEdit={this.props.isEdit}
          onRef={(ref) => {
            this.$operateFormC = ref;
          }}
        />
      </ModalC>
    );
  }
}
